package com.david.controller.api;

import com.david.annotation.CurrentAdmin;
import com.david.dao.WebSiteDao;
import com.david.entity.AdminInfo;
import com.david.entity.WebSite;
import com.david.model.api.Response;
import com.david.model.api.UpdatePassWordRequest;
import com.david.service.AdminInfoService;
import com.david.service.WebSiteService;
import com.david.utils.WebAuthentication;
import com.google.common.collect.ImmutableBiMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/settings/")
public class SettingsController {

    @Autowired
    AdminInfoService adminInfoService;

    @Autowired
    WebSiteService webSiteService;

    @GetMapping("getAdminList")
    @ApiOperation(value="查看管理员列表",notes = "管理员列表",tags = "admin")
    public Response<Object> getAdminList(){
        Response<Object> result = new Response<>();

        List<AdminInfo> adminList = adminInfoService.findAll();

        result.data = adminList;

        return result;
    }

    @PostMapping("updatePassword")
    @ApiOperation(value = "修改密码", notes = "修改密码", tags = "admin")
    public Response<Map> updatePassword(@CurrentAdmin AdminInfo adminInfo, @RequestBody UpdatePassWordRequest request) throws Exception {

        Response<Map> result = new Response<>();

        adminInfo.setPassWord(WebAuthentication.encryptionData(request.getOldPassword()));

        AdminInfo oldInfo = adminInfoService.CheckOldPass(adminInfo);

        if (oldInfo == null) {
            result.data = ImmutableBiMap.of("result", false, "errMsg", "原始密码不正确");
            return result;
        }

        adminInfo.setPassWord(WebAuthentication.encryptionData(request.getNewPassword()));

        int passResult = adminInfoService.UpdatePassWord(adminInfo);
        if (passResult > 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false, "errMsg", "设置失败，请稍后再试");
        }

        return result;
    }

    @GetMapping("WebSiteInfo")
    @ApiOperation(value = "获取基本信息", notes = "获取基本信息", tags = "website")
    public Response<Object> WebSiteInfo() {

        Response<Object> result = new Response<>();

        WebSite websiteInfo = webSiteService.GetWebSite();

        result.data = websiteInfo;

        return result;
    }

    @PostMapping("updateWebSite")
    @ApiOperation(value = "修改基本信息", notes = "修改基本信息", tags = "website")
    public Response<Map> updateWebSite(@RequestBody WebSite request) {

        Response<Map> result = new Response<>();

        if (request == null) {
            result.data = ImmutableBiMap.of("result", false, "errMsg", "请输入基本信息");
            return result;
        }

        int update = webSiteService.UpdateWebSite(request);
        if (update > 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false, "errMsg", "设置失败，请稍后再试");
        }

        return result;
    }
}
